﻿<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeFile="ChiTietPhieuNhapHang.aspx.cs"
    Inherits="WebTeamplate.Admin_ChiTietPhieuNhapHang" ValidateRequest="false" %>

<%@ Import Namespace="WebTeamplate" %>
<%@ Register Assembly="Control" Namespace="Kng.Controls" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <script language="javascript" type="text/javascript" src="../Js/actb.js"></script>

        <script language="javascript" type="text/javascript" src="../Js/commonAutocomplete.js"></script>

        <script src="../Js/Popup.js"></script>

        <script src="../Js/BasicFunction.js"></script>

        <script src="../Js/ClientScript.js"></script>

        <script src="../Js/formatNumber.js"></script>

        <link href="../Js/calendar/calendar-win2k-cold-1.css" rel="stylesheet" type="text/css" />

        <script src="../Js/DateFunction.js"></script>

        <script type="text/javascript" src="../Js/calendar/calendar.js"></script>

        <script type="text/javascript" src="../Js/calendar/calendar-setup.js" language="javascript"></script>

        <script type="text/javascript" src="../Js/calendar/lang/calendar-en.js"></script>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <table class="listForm" border="0" cellpadding="0" cellspacing="0" width="1200" align="center">
            <tr>
                <td colspan="8" class="detailedViewHeader">
                    <b>Quản lý phiếu nhập hàng</b>
                    <asp:Button ID="btnDefault" runat="server" CssClass="buttonHidden" Text="" CausesValidation="false"
                        OnClientClick="return DefaultEnter();" />
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="8" class="dvtCellInfo">
                    (<span class="commentRequireInput">*</span>): <span class="commentRequireInputText">
                        <%=Resources.Resource.LABEL_COMMENT_REQUIRE %>
                    </span>
                </td>
            </tr>
            <tr>
                <td align="left" colspan="8" class="dvtCellErrorMessage">
                    <asp:Label ID="lblErrorMessage" runat="server" CssClass="validateSumary"></asp:Label>
                    <div id="divFormMatHang" style="display: none;">
                        <table cellspacing="0" cellpadding="0" border="0" class="tabledialogWindow">
                            <tr>
                                <td>
                                    <center>
                                        <asp:UpdatePanel ID="upnDocument" runat="server" UpdateMode="Conditional">
                                            <ContentTemplate>
                                                <table style="width: 550px; padding-top: 15px; padding-bottom: 15px; border: 1px solid black;"
                                                    class="text-normal">
                                                    <tr>
                                                        <td>
                                                            <input type="hidden" id="hidTemp" value="" runat="server" />
                                                            <asp:ValidationSummary ID="vsumMatHang" CssClass="validateSumary" DisplayMode="BulletList"
                                                                runat="server" ShowMessageBox="true" ShowSummary="false" />
                                                            <asp:Label ID="lblvMatHangError" runat="server" CssClass="validateSumary"></asp:Label>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <table class="listForm" border="0" cellpadding="3" cellspacing="2" width="100%">
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel" width="150px">
                                                                        <asp:Label ID="lblFormMHMaMatHang" runat="server" Text="Mã mặt hàng"></asp:Label>(<span
                                                                            class="commentRequireInput">*</span>):
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:TextBox ID="txtFormMHMaMatHang" runat="server" MaxLength="6" Text="000001" Enabled="false"
                                                                            CssClass="detailedViewTextBoxList_Disable1 ctrListForm" Width="80px">
                                                                        </asp:TextBox>
                                                                        <asp:RequiredFieldValidator ID="refvFormMHMaMatHang" ControlToValidate="txtFormMHMaMatHang"
                                                                            runat="server" SetFocusOnError="true">*</asp:RequiredFieldValidator>
                                                                    </td>
                                                                </tr>
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel">
                                                                        <asp:Label ID="lblFormMHLoaiMatHang" runat="server" Text="Loại mặt hàng"></asp:Label>(<span
                                                                            class="commentRequireInput">*</span>):
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:DropDownList ID="drlFormMHLoaiMH" runat="server" CssClass="listForm ctrListForm">
                                                                        </asp:DropDownList>
                                                                        <asp:RequiredFieldValidator ID="refvFormMHLoaiMH" ControlToValidate="drlFormMHLoaiMH"
                                                                            runat="server" SetFocusOnError="true">*</asp:RequiredFieldValidator>
                                                                    </td>
                                                                </tr>
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel">
                                                                        <asp:Label ID="lblFormMHTenMatHang" runat="server" Text="Tên mặt hàng"></asp:Label>(<span
                                                                            class="commentRequireInput">*</span>):
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:TextBox ID="txtFormMHTenMH" runat="server" Width="250px" MaxLength="100" CssClass='detailedViewTextBoxList ctrListForm'
                                                                            onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);'></asp:TextBox>
                                                                        <asp:RequiredFieldValidator ID="refvFormMHTenMH" ControlToValidate="txtFormMHTenMH"
                                                                            runat="server" SetFocusOnError="true" Display="Dynamic">*</asp:RequiredFieldValidator>
                                                                    </td>
                                                                </tr>
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel">
                                                                        <asp:Label ID="lblFormMHHieu" runat="server" Text="Hiệu"></asp:Label>:
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:TextBox ID="txtFormMHHieu" runat="server" Width="250px" MaxLength="100" CssClass='detailedViewTextBoxList ctrListForm'
                                                                            onfocus='TextBoxLeftFocus(this);' onblur='TextBoxLeftBlur(this);'></asp:TextBox>
                                                                    </td>
                                                                </tr>
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel">
                                                                        <asp:Label ID="lblFormMHNuocSX" runat="server" Text="Nước SX"></asp:Label>:
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:DropDownList ID="drlFormMHNuocSX" runat="server" CssClass="listForm ctrListForm">
                                                                        </asp:DropDownList>
                                                                    </td>
                                                                </tr>
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel">
                                                                        <asp:Label ID="lblFormMHGiaBan" runat="server" Text="Giá bán"></asp:Label>:
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:TextBox ID="txtFormMHGiaBan" runat="server" Width="100px" MaxLength="12" CssClass='detailedViewTextBoxList1 ctrListForm txtNumber'
                                                                            onfocus='TextBoxFocus(this);' onblur="TextBoxBlurGroupNumber(this);"></asp:TextBox>
                                                                        <asp:CustomValidator ID="cusvFormMHGiaBanValid" runat="server" SetFocusOnError="true"
                                                                            ClientValidationFunction="CheckCFormMHGiaBanValid" EnableClientScript="true"
                                                                            ErrorMessage="RequiredFieldValidator" ControlToValidate="txtFormMHGiaBan" OnServerValidate="CheckSFormMHGiaBanValid"
                                                                            Display="Dynamic">*</asp:CustomValidator>
                                                                    </td>
                                                                </tr>
                                                                <tr class="rowDetail">
                                                                    <td class="dvtCellLabel">
                                                                        <asp:Label ID="lblDVT" runat="server" Text="Đơn vị tính"></asp:Label>:
                                                                    </td>
                                                                    <td class="dvtCellInfo">
                                                                        <asp:DropDownList ID="drlFormMHDVT" runat="server" CssClass="listForm ctrListForm">
                                                                        </asp:DropDownList>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td>
                                                                    </td>
                                                                    <td style="height: 40px;" valign="bottom" align="left">
                                                                        <asp:Button ID="btnFormMHSave" runat="server" CssClass="crmbutton  listForm edit"
                                                                            OnClick="btnFormMHSave_Click" Text="Save" CausesValidation="true" />
                                                                        <input type="button" value='Close' id="btnCloseFormMatHang" class="crmbutton 
listForm edit" onclick="ClosePopupMatHang();" />
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                    </center>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div id="divFormBackground" class="lightbox" style="display: none;" onclick="ClickFormMatHang(this);">
                        &nbsp;</div>
                </td>
            </tr>
            <tr>
                <td class="dvtCellLabel">
                    <asp:Label ID="lblChungXe" runat="server" Text="Chủng xe"></asp:Label>(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="130px" align="left">
                    <asp:DropDownList ID="drlChungXe" CssClass="listForm ctrListForm" runat="server"
                        Width="120px" AutoPostBack="True" OnSelectedIndexChanged="drlChungXe_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
                <td class="dvtCellLabel">
                    <asp:Label ID="lblNhaCungCap" runat="server" Text="Nhà cung cấp"></asp:Label>(<span
                        class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" colspan="3" align="left">
                    <asp:TextBox ID="txtNhaCungCap" runat="server" Width="514px" MaxLength="128" onblur="this.className='detailedViewTextBoxList1'"
                        onfocus="this.className='detailedViewTextBoxListOn1'" CssClass="detailedViewTextBoxList1"></asp:TextBox>
                </td>
                <td>
                </td>
                <td>
                </td>
            </tr>
            <tr class="rowDetail">
                <td class="dvtCellLabel" width="90px">
                    <asp:Label ID="lblKho" runat="server" Text="Khu"></asp:Label>(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="130px" align="left">
                    <asp:DropDownList ID="drlKho" CssClass="listForm ctrListForm" runat="server" Width="120px">
                    </asp:DropDownList>
                </td>
                <td width="120px" align="right" class="dvtCellLabel">
                    Ngày nhập(<span class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo" width="120px" align="left">
                    <asp:TextBox ID="txtNgayNhap" runat="server" Width="90px" MaxLength="10" onblur="this.className='detailedViewTextBoxList1'"
                        onfocus="this.className='detailedViewTextBoxListOn1'" CssClass="detailedViewTextBoxList1"></asp:TextBox>
                    <img id="imgNgayNhap" alt="" title="Ngày nhập" style="cursor: hand; text-align: left;
                        vertical-align: middle; margin-bottom: 4px;" src="../images/imgCalendar.gif" />
                </td>
                <td width="170px" align="right" class="dvtCellLabel">
                    <asp:Label ID="lblNguoiNhap" runat="server" Text="Người nhập/ cập nhật"></asp:Label>(<span
                        class="commentRequireInput">*</span>):
                </td>
                <td class="dvtCellInfo">
                    <asp:TextBox ID="txtNguoiNhap" runat="server" Width="180px" MaxLength="100" Enabled="false"
                        Text="Nguyen Van Ty" CssClass="detailedViewTextBoxList_Disable1 ctrListForm"></asp:TextBox>
                       
                        &nbsp;&nbsp;&nbsp;&nbsp;<asp:CheckBox ID="chkCommit" Checked="true" runat="server" Text="Chốt sổ" /> 
                </td>
            </tr>
            <tr>
                <td colspan="8" class="dvtCellInfo">
                    <hr size="1" noshade="noshade">
                </td>
            </tr>
            <tr class="rowDetail" align="right" style="display:;">
                <td colspan="8" class="dvtCellListInfo">
                    <table width="100%">
                        <tr>
                            <td align="right" width="86%">
                                Cộng thành tiền
                            </td>
                            <td align="left">
                                <input type="text" runat="server" class="detailedViewTextBoxList_Disable1 ctrListForm txtNumber"
                                    style="width: 98%;" disabled="disabled" id="txtTongThanhTien" maxlength="14"
                                    value="0" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="8" class="dvtCellListInfo" id="tdForm" runat="server">
                </td>
            </tr>
            <tr class="rowDetail" align="right">
                <td colspan="8" class="dvtCellListInfo">
                    <table width="100%" cellspacing="0" cellpadding="0" border="0" class="listForm">
                        <tr>
                            <td align="right">
                                <input type="button" value="Thêm dòng nhập" onclick="AddRow();" id="btnAddRow">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="height: 40px;" valign="bottom" colspan="8" align="center">
                    <asp:Button ID="btnSave" runat="server" CssClass="crmbutton 
listForm edit" Text="Save" OnClick="btnSave_Click" CausesValidation="false" OnClientClick="return ValidatePhieuNhapHang()" />
                    <asp:Button ID="btnCreateBarCode" runat="server" CssClass="crmbutton  
listForm edit" Text="Tạo mã vạch" CausesValidation="false" OnClick="btnCreateBarCode_Click" Width="90px" />
                    <asp:Button ID="btnXemBanIn" runat="server" CssClass="crmbutton
listForm edit" Text="Xem chi tiết" CausesValidation="false" Width="90px" OnClick="btnXemBanIn_Click" />
                    <asp:Button ID="btnDelete" runat="server" CssClass="crmbutton
listForm edit" Text="Xóa" CausesValidation="false" Width="70px" OnClientClick="return ConfirmDelete();"
                        OnClick="btnDelete_Click" />
                    <asp:Button ID="btnReset" runat="server" CssClass="crmbutton 
listForm edit" Text="Save" OnClick="btnReset_Click" CausesValidation="false" />
                    <asp:Button ID="btnCacel" runat="server" CssClass="crmbutton 
listForm edit" Text="Cancel" OnClick="btnCacel_Click" CausesValidation="false" />
                </td>
            </tr>
        </table>
        <input type="hidden" id="hidTotalRow" value="" runat="server" />
        <input type="hidden" id="hidMaxRowShow" value="" runat="server" />
        <input type="hidden" id="hidCurrentRowShow" value="" runat="server" />
        <input type="hidden" id="hidOptionLoaiMH" value="" runat="server" />
        <input type="hidden" id="hidOptionDVT" value="" runat="server" />
        <input type="hidden" id="hidRowValid" value="" runat="server" />
        <input type="hidden" id="hidNow" value="" runat="server" />

        <script language="javascript" type="text/javascript">
            var txtNgayNhap = document.getElementById("<%= txtNgayNhap.ClientID %>");
            SetupCalendar(txtNgayNhap.id, "imgNgayNhap");     
        </script>

        <asp:Literal ID="ltrScript" runat="server">
        </asp:Literal>

        <script type="text/javascript">
            function CheckCFormMHGiaBanValid(sender, args) {
                // alert('Check Client');

                args.IsValid = CheckCGiaIsValid(args.Value);

            }


            var alredyFocus = false;
            function RowHover(obj) {
                if (obj)
                    obj.className = 'lvtColDataHover';
            }
            function RowOut(obj) {
                if (obj)
                    obj.className = 'lvtColData';
            }
            function TextBoxFocus(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxListOn1 txtNumber';
            }
            function TextBoxBlurGroupNumber(obj) {

                if (obj) {
                    obj.className = 'detailedViewTextBoxList1 txtNumber';
                    obj.value = formatNumberAutoSeparate(obj.value);
                }
            }
            function TextBoxBlur(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxList1 txtNumber';
            }
            function TextBoxLeftFocus(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxListOn1';
            }
            function TextBoxLeftBlur(obj) {
                if (obj)
                    obj.className = 'detailedViewTextBoxList1';
            }
            function ThemMatHang(row) {
                //clear Form MH
                rowMT = row;
                document.getElementById('<%=txtFormMHTenMH.ClientID %>').value = '';
                document.getElementById('<%=txtFormMHHieu.ClientID %>').value = '';
                document.getElementById('<%=drlFormMHNuocSX.ClientID %>').value = '';
                document.getElementById('<%=txtFormMHGiaBan.ClientID %>').value = '';
                document.getElementById('<%=drlFormMHDVT.ClientID %>').value = '';
                document.getElementById('<%=lblvMatHangError.ClientID %>').innerText = '';
                alredyFocus = false;
                var divFormMatHang = document.getElementById('divFormMatHang');
                var divFormBackground = document.getElementById('divFormBackground')
                var btn = document.getElementById('<%=drlFormMHLoaiMH.ClientID %>');

                f_putScreen(divFormMatHang, divFormBackground, btn, true);
                //defaulr select value LoaiMH
                var loaiMH = document.getElementById("drlLoaiMatHang_" + row).value;
                document.getElementById('<%=drlFormMHLoaiMH.ClientID %>').value = loaiMH;
                GetProductCode();
            }
            function DeleteRow(index) {
                //var yes = confirm('Bạn muốn xóa dòng này?');
                var yes = true;
                if (yes) {
                    if (document.getElementById('tr_' + index)) {

                        document.getElementById('tr_' + index).style.display = 'none';
                        var hidCurrentRowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                        hidCurrentRowShow = parseInt(hidCurrentRowShow, 0);
                        hidCurrentRowShow = hidCurrentRowShow - 1;
                        document.getElementById('<%=hidCurrentRowShow.ClientID %>').value = hidCurrentRowShow;

                        var oldValue = document.getElementById("txtThanhTien_" + index).value;
                        oldValue = ReplaceAllChar(removeComma(oldValue),",",".");
                        oldValue = parseFloat(oldValue);
                        if (isNaN(oldValue))
                            oldValue = 0;
                        var tongtien = document.getElementById('<%=txtTongThanhTien.ClientID %>').value;
                        tongtien = ReplaceAllChar(removeComma(tongtien), ",", ".");
                        tongtien = parseFloat(tongtien);
                        if (isNaN(tongtien))
                            tongtien = 0;
                        tongtien = tongtien - oldValue;
                        document.getElementById('<%=txtTongThanhTien.ClientID %>').value = formatNumberAutoSeparate(ReplaceAllChar(tongtien.toString(),".",","));
                    }
                }
            }
            var rowMT = 1;
            function ChangeLoaiHH(index, value) {

                //get list DVT for this
                rowMT = index;
                Admin_ChiTietPhieuNhapHang.GeMatHangLoaiMH(value, GenareteMatHang);
                return false;

            }



            function GenareteMatHang(response) {

                //response.value

                if (rowMT > 0) {
                    document.getElementById('td_' + rowMT).innerHTML = "<select style='width: 84%' OnKeyUp='ChangeMatHang(" + rowMT + ",this.value)' onChange='ChangeMatHang(" + rowMT + ",this.value)' id='drlMatHang_" + rowMT + "' class='listForm ctrListForm'>" + response.value + "</select>&nbsp;&nbsp;<input type='button' class='btnAddMH ctrListForm' value='+' onclick='ThemMatHang(" + rowMT + ");' id='btnAdd'>";
                    document.getElementById('txtNuocSX_' + rowMT).value = '';
                    document.getElementById('txtHieu_' + rowMT).value = '';
                    document.getElementById('<%=hidTemp.ClientID %>').value = response.value;
                }
                rowMT = 0;
            }
            function GetMHByname(value, index) {
                var maMH = Right(value, 6);
                rowMT = index;

                Admin_ChiTietPhieuNhapHang.GeMatHangInfor(maMH, GetMatHangInfor);
                /* document.getElementById('txtMaMH_' + index).value = maMH;*/
            }
            function ChangeMatHang(index, value) {

                rowMT = index;
                Admin_ChiTietPhieuNhapHang.GeMatHangInfor(value, GetMatHangInfor);
                document.getElementById('txtMaMH_' + index).value = value;
                // document.getElementById("drlLoaiMatHang_" + index).value = value;
                return false;
            }
            function GetMatHangInfor(response) {

                if (rowMT > 0) {
                    var arr = new Array();

                    arr = response.value.split('*+*');

                    if (arr.length > 1) {
                        var maPT = GetValueArray(arr, 1);
                        var maMH = GetValueArray(arr, 0);
                        SetControlvalue(document.getElementById('txtMaMH_' + rowMT), maMH);
                        SetControlvalue(document.getElementById('txtMaPT_' + rowMT), GetValueArray(arr, 1));
                        SetControlvalue(document.getElementById('txtXe_' + rowMT), GetValueArray(arr, 2));
                        SetControlvalue(document.getElementById('txtNuocSX_' + rowMT), GetValueArray(arr, 3));
                        SetControlvalue(document.getElementById('txtHieu_' + rowMT), GetValueArray(arr, 4));
                        SetControlvalue(document.getElementById('drlDonViTinh_' + rowMT), GetValueArray(arr, 5));
                        //  document.getElementById('txtGiaNhap_' + rowMT).value = '1000';
                        //objMatHang.MaMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" +   objMatHang.TenNuocSX + "*+*" + objMatHang.Hieu + "*+*" + objMatHang.DVT.ToString();
                        if (maMH != '') {
                            if (maPT == '') {
                                document.getElementById('txtMaPT_' + rowMT).disabled = true;
                                document.getElementById('txtMaPT_' + rowMT).className = 'detailedViewTextBoxList_Disable1 ctrListForm';
                            }
                            else {
                                document.getElementById('txtMaPT_' + rowMT).className = 'detailedViewTextBoxList_Disable ctrListForm';
                                document.getElementById('txtMaPT_' + rowMT).disabled = false;
                            }
                        }
                        else {
                            document.getElementById('txtMaPT_' + rowMT).className = 'detailedViewTextBoxList_Disable ctrListForm';
                            document.getElementById('txtMaPT_' + rowMT).disabled = false;
                        }

                    }
                    else SetControlvalue(document.getElementById('txtMaMH_' + rowMT), '');
                }
                rowMT = 0;
            }
            function SetControlvalue(ctr, value) {
                try {
                    if (ctr)
                        ctr.value = value;
                }
                catch (e) {
                }
            }
            function ClickFormMatHang(obj) {
                if (!alredyFocus) {
                    var btn = document.getElementById('btnCloseFormMatHang');
                    btn.focus();
                }
            }
            function ClosePopupMatHang() {
                var divFormMatHang = document.getElementById('divFormMatHang');
                var divFormBackground = document.getElementById('divFormBackground')
                var btn = document.getElementById('<%=drlFormMHLoaiMH.ClientID %>');
                f_putScreen(divFormMatHang, divFormBackground, btn, false);
                alredyFocus = true;
                return false;
            }
            function GetProductCode() {
                Admin_ChiTietPhieuNhapHang.GenareteProductCode(SetProductCode);
                return false;
            }
            function SetProductCode(response) {

                document.getElementById('<%=txtFormMHMaMatHang.ClientID %>').value = response.value;

            }
            function InsertMatHangOK(loaiMH, maMH, tenMH, hieu, nuocSX, dvt) {

                ClosePopupMatHang(); //close Popup
                //default value to list control
                var matHangOption = document.getElementById('<%=hidTemp.ClientID %>').value;
                document.getElementById('td_' + rowMT).innerHTML = "<select style='width:84%' onkeyup='this.blur();this.focus();' onChange='ChangeMatHang(" + rowMT + ",this.value)' id='drlMatHang_" + rowMT + "' class='listForm ctrListFormlistForm ctrListForm'>" + matHangOption + "</select>&nbsp;&nbsp;<input type='button' class='btnAddMH ctrListForm' value='+' onclick='ThemMatHang(" + rowMT + ");' id='btnAdd'>";
                document.getElementById('drlLoaiMatHang_' + rowMT).value = loaiMH;
                document.getElementById('txtMaMH_' + rowMT).value = maMH;
                document.getElementById('txtNuocSX_' + rowMT).value = nuocSX;
                document.getElementById('txtHieu_' + rowMT).value = hieu;
                document.getElementById('drlDonViTinh_' + rowMT).value = dvt;
                rowMT = 0;
                /*ChangeLoaiHH(indexRow, loaiMH);
                document.getElementById('drlMatHang_' + indexRow).value=maMH;
                ChangeMatHang(indexRow, maMH);*/


            }


            function AddRow() {

                var maxRow = document.getElementById('<%=hidMaxRowShow.ClientID %>').value;
                maxRow = parseInt(maxRow, 0);
                maxRow += 1;

                var hidCurrentRowShow = document.getElementById('<%=hidCurrentRowShow.ClientID %>').value;
                hidCurrentRowShow = parseInt(hidCurrentRowShow, 0);
                hidCurrentRowShow = hidCurrentRowShow + 1;
                if (hidCurrentRowShow > maxRow)
                    return;

                document.getElementById('tr_' + maxRow).style.display = ''
                document.getElementById('<%=hidMaxRowShow.ClientID %>').value = maxRow;
                document.getElementById('<%=hidCurrentRowShow.ClientID %>').value = hidCurrentRowShow;
                document.getElementById('txtTenMH_' + maxRow).focus();

            }
            window.scrollTo = function() {

            }
            function ValidatePhieuNhapHang() {

                var isCommit = '';
                if (document.getElementById('<%=chkCommit.ClientID %>'))
                    isCommit=document.getElementById('<%=chkCommit.ClientID %>').checked;
               
                
                //check require kho
                var kho = document.getElementById('<%=drlKho.ClientID %>').value;
                if (kho == '') {
                    alert('Vui lòng chọn khu');
                    document.getElementById('<%=drlKho.ClientID %>').focus();
                    return false;
                }

                var chungxe = document.getElementById('<%=drlChungXe.ClientID %>').value;
                if (chungxe == '') {
                    alert('Vui lòng chọn chủng xe');
                    document.getElementById('<%=drlChungXe.ClientID %>').focus();
                    return false;
                }


                var ngayNhap = document.getElementById('<%=txtNgayNhap.ClientID %>').value;
                if (!isDate(ngayNhap, "dd/MM/yyyy")) {
                    alert('Ngày nhập không hợ lệ');
                    document.getElementById('<%=txtNgayNhap.ClientID %>').focus();
                    return false;
                }
                //check valida date here
                //check Phieu Nhap Hang Data

                var data = '';
                var totalRow = document.getElementById('<%=hidTotalRow.ClientID %>').value;
                totalRow = parseInt(totalRow, 0);
                var inValidData = 1;
                for (var i = 1; i <= totalRow; i++) {
                    var tr = document.getElementById('tr_' + i);
                    if (tr) {
                        if (tr.style.display == '') {
                            var matHang = document.getElementById('txtMaMH_' + i).value;
                            if (matHang != '') {
                                if (!ValidataRow(i)) {
                                    alert('Vui lòng kiểm tra lại dữ liệu nhập tại dòng ' + inValidData);
                                    return false;
                                }
                                else {

                                    //MaMH|DVT|SL|GiaNhap
                                    var txtMaMH = document.getElementById('txtMaMH_' + i).value;
                                    var soluong = document.getElementById('txtSoLuong_' + i).value;
                                    var dvt = document.getElementById('drlDonViTinh_' + i).value;
                                    var giaNhap = document.getElementById('txtGiaNhap_' + i).value;
                                    var curency = document.getElementById('drlCurency_' + i).value;
                                    data += txtMaMH + ";" + soluong + ";" + dvt + ";" + ReplaceAllChar(removeComma(giaNhap),",",".") + ";" + curency + "*";
                                }

                            }
                            inValidData++;

                        }
                    }
                }
                if (data == '') {
                    document.getElementById('<%=btnSave.ClientID %>').focus();
                    alert('Vui lòng nhập thông tin từng mặt hàng cho phiếu nhập hàng');
                    return false;
                }

                var nguoiNhap = '<%=userNameLogin %>';
                var command = '<%=Command %>';
                var MaPhieuPhap = '<%=MaPhieuPhap %>';
                var nhacCC = document.getElementById('<%=txtNhaCungCap.ClientID %>').value;
                var now = document.getElementById('<%=hidNow.ClientID %>').value;
                //Get Data and Insert DB
                
                Admin_ChiTietPhieuNhapHang.InsertData(command, MaPhieuPhap, kho, nguoiNhap, ngayNhap, now, data, nhacCC,isCommit, InsertPhieuNhapHangResult);
                return false;
            }

            function InsertPhieuNhapHangResult(response) {
                if (response.value == '0') {
                    alert('Có lỗi xảy ra trong quá trình truy cập dữ liệu. Vui lòng thử lai.!');
                    return false;
                }
                else {

                    window.location.href = 'ViewPhieuNhapHang.aspx?id=' + response.value;

                }

            }

            function ValidataRow(index) {
                
                //check require MatHang
                // var matHang = document.getElementById('txtMaMH_' + index).value;
                // if (matHang == '') {
                //document.getElementById('txtTenMH_' + index).focus();
                //  return true;
                //}
                //check require SoLuong
                var soluong = document.getElementById('txtSoLuong_' + index).value;
                if (!IsValidInt(soluong)) {
                    document.getElementById('txtSoLuong_' + index).focus();
                    return false;
                }

                //IsValidInt
                var dvt = document.getElementById('drlDonViTinh_' + index).value;
                if (dvt == '') {
                    document.getElementById('drlDonViTinh_' + index).focus();
                    return false;
                }
                //check gianhap is valid
                var giaNhap = document.getElementById('txtGiaNhap_' + index).value;
                if (giaNhap == '') {
                    document.getElementById('txtGiaNhap_' + index).focus();
                    return false;
                }

                if (!CheckCGiaIsValid(giaNhap)) {
                    document.getElementById('txtGiaNhap_' + index).focus();
                    return false;
                }
                return true;

            }
            function ConfirmDelete() {
                var y = confirm('Bạn muốn xóa phếu nhập hàng này');
                if (y) return true;
                return false;
            }
            function GetThanhTien(row) {
                debugger;
                var dongia = document.getElementById('txtGiaNhap_' + row).value;
                dongia = ReplaceAllChar(removeComma(dongia),",",".");
                dongia = parseFloat(dongia);
                if (isNaN(dongia))
                    dongia = 0;
                var oldValue = document.getElementById("txtThanhTien_" + row).value;
                oldValue = ReplaceAllChar(removeComma(oldValue), ",", ".");
                oldValue = parseFloat(oldValue);
                if (isNaN(oldValue))
                    oldValue = 0;
                var soLuong = document.getElementById('txtSoLuong_' + row).value;
                soLuong = parseInt(soLuong);
                if (isNaN(soLuong))
                    soLuong = 0;
                dongia = soLuong * dongia;
                if (dongia > 0)
                    document.getElementById("txtThanhTien_" + row).value = formatNumberAutoSeparate(ReplaceAllChar(dongia.toString(),".",","));
                else document.getElementById("txtThanhTien_" + row).value = '';

                var tongtien = document.getElementById('<%=txtTongThanhTien.ClientID %>').value;
                tongtien = parseFloat(ReplaceAllChar(removeComma(tongtien), ",", "."));
                if (isNaN(tongtien))
                    tongtien = 0;
                tongtien = tongtien - oldValue;
                tongtien = tongtien + dongia;
                if (tongtien > 0)
                    document.getElementById('<%=txtTongThanhTien.ClientID %>').value = formatNumberAutoSeparate(ReplaceAllChar(tongtien.toString(),".",","));
                else document.getElementById('<%=txtTongThanhTien.ClientID %>').value = '';
            }
            function ChangeCurency(value) {
                var lstCtr = document.getElementsByTagName('select');
                var ctr = ''
                //                
                //                ctr = lstCtr[0];
                //                alert(ctr.id);
                var index = -1;
                for (var i = 0; i < lstCtr.length; i++) {

                    ctr = lstCtr[i];
                    index = ctr.id.indexOf('drlCurency_');
                    if (index >= 0) {
                        ctr.value = value;
                    }
                }
            }
            function DefaultEnter() {
                return false;
            }
            function GetMHByMaPT(value, row) {

                rowMT = row;
                Admin_ChiTietPhieuNhapHang.GeMatHangInforByMaPT(value, GetMatHangInforByMaPT);
            }
            function GetMatHangInforByMaPT(response) {

                if (rowMT > 0) {
                    var arr = new Array();

                    arr = response.value.split('*+*');

                    if (arr.length > 1) {

                        SetControlvalue(document.getElementById('txtMaMH_' + rowMT), GetValueArray(arr, 0));
                        SetControlvalue(document.getElementById('txtMaPT_' + rowMT), GetValueArray(arr, 1));
                        SetControlvalue(document.getElementById('txtXe_' + rowMT), GetValueArray(arr, 2));
                        SetControlvalue(document.getElementById('txtNuocSX_' + rowMT), GetValueArray(arr, 3));
                        SetControlvalue(document.getElementById('txtHieu_' + rowMT), GetValueArray(arr, 4));
                        SetControlvalue(document.getElementById('drlDonViTinh_' + rowMT), GetValueArray(arr, 5));
                        SetControlvalue(document.getElementById('txtTenMH_' + rowMT), GetValueArray(arr, 6));
                        //  document.getElementById('txtGiaNhap_' + rowMT).value = '1000';
                        //objMatHang.MaMatHang + "*+*" + objMatHang.MaPhuTung + "*+*" + xe + "*+*" +   objMatHang.TenNuocSX + "*+*" + objMatHang.Hieu + "*+*" + objMatHang.DVT.ToString();

                    }

                }
                rowMT = 0;
            }
            function GetValueArray(arr, index) {
                try {
                    return arr[index].toString()
                }
                catch (e) {
                    return '';
                }
            }
            function ResetCurency(curency, maxRow) {
                var maxIndex = parseInt(maxRow);
                var row = '';
                for (var i = 1; i <= maxIndex; i++) {
                    row = document.getElementById('tr_' + i);
                    if (row.style.display == 'none') {
                        document.getElementById('drlCurency_' + i).value = curency;
                    }
                }

            }
        </script>

    </div>
    </form>
</body>
</html>
